package logic

import (
	"context"
	"errors"
	"product-payment/models/mysql"

	"product-payment/prorpc/internal/svc"
	"product-payment/prorpc/prorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *prorpc.ShopAddRequest) (*prorpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var shop mysql.Shop
	mysql.DB.Debug().Where("id = ?", shop.ID).Find(&shop)
	if shop.ID == 0 {
		return nil, errors.New("当前商品不存在")
	}

	shop = mysql.Shop{
		Name:  in.Name,
		Price: float64(in.Price),
		Num:   int(in.Num),
	}
	mysql.DB.Debug().Create(&shop)

	return &prorpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
